package com.coms514.scheduleme;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class ScheduleMeDatabase {
	public static final String TAG = "ScheduleMeDatabase";
	
	/* for Singleton Instance */
	private static ScheduleMeDatabase database;
	
	/* Database Name, Table Names */
	public static String DATABASE_NAME = "schedulme.db";
	public static String TABLE_CAREGIVER = "CAREGIVER";
	
	/* append TABLE_EVENTLIST, TABLE_FEEDBACK */
	
	/* Database Version */
	public static int DATABASE_VERSION = 1;
	
	private DatabaseHelper dbHelper;
	private SQLiteDatabase db;
	private Context context;
	
	private ScheduleMeDatabase(Context context) {
		this.context = context;
	}
	
	public static ScheduleMeDatabase getInstance(Context context) {
		if (database == null) {
			database = new ScheduleMeDatabase(context);
		}
		return database;
	}
	
	public boolean open() {
		println("opening database [" + DATABASE_NAME + "].");
		
		dbHelper = new DatabaseHelper(context);
		db = dbHelper.getWritableDatabase();
		
		return true;
	}
	
	public void close() {
		println("closing database [" + DATABASE_NAME + "].");
		
		db.close();
		database = null;
	}
	
	public Cursor rawQuery(String sql) {
		println("\nrawQuery called.\n");
		
		Cursor c1 = null;
		try {
			c1 = db.rawQuery(sql, null);
			println("cursor count: " + c1.getCount());
		} catch (Exception ex) {
			Log.e(TAG, "Exception in rawQuery", ex);
		}
		return c1;
	}
	
	public boolean execSQL(String sql) {
		println("\nexecSQL called.\n");
		
		try {
			Log.d(TAG, "SQL: " + sql);
			db.execSQL(sql);			
		} catch (Exception ex) {
			Log.e(TAG, "Exception in execSQL", ex);
			return false;
		}
		return true;
	}
	
	/* define SQLiteOpenHelper class */
	private class DatabaseHelper extends SQLiteOpenHelper {
		public DatabaseHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			println("creating table [" + TABLE_CAREGIVER + "].");
			
			String DROP_SQL = "drop table if exists " + TABLE_CAREGIVER;
			try {
				db.execSQL(DROP_SQL);				
			} catch (Exception ex) {
				Log.e(TAG, "Exception in DROP_SQL", ex);
			}
			
			String CREATE_SQL = "create table " + TABLE_CAREGIVER + "("
					+ " _id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "
					+ " name text,"
					+ " email text,"
					+ " phone text"
					+ ")";
			try {
				db.execSQL(CREATE_SQL);				
			} catch (Exception ex) {
				Log.e(TAG, "Exception in CREATE_SQL", ex);
			}
		}

		public void onOpen(SQLiteDatabase db) {
			println("opened database [" + DATABASE_NAME + "].");
		}
		
		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			println("Upgrading database from version " + oldVersion + " to " + newVersion + ".");
			
			if (oldVersion < 2) {
				
			}
		}
	}
	
	/* for logging */
	private void println(String msg) {
		Log.d(TAG, msg);
	}
	
}
